import 'global/vue3.2.11.js';
import biPlug from '../bi-plug/install.js';
import adminPlug from './adminPlug.js';
import User from './user.js';
import Status from './status.js'; 
import { ajax,assign } from './global.js';
import { loading,dialog } from '../bi-plug/plug.js';

import modifyUserPasswordDialog from '../component/dialog/modify-password.vue';
import userFileDialog from '../component/dialog/user-file.vue';
import systemFileDialog from '../component/dialog/system-file.vue';


class admin{

    app = '';
    controller = '';
    action = '';
    user = new User();
    status ;
    baseUrl = '/index.php/app';
    uploadBaseUrl = '/upload';
    componentList  = {};
    directiveList  = {};
    data = {};
    constructor(){


        
    }


    /**
     * 注册一个组件
     * @param {*} name 
     * @param {*} config 
     */
    component(name,config){
        if(this.componentList[name]){
            console.log(name + '组件已经存在');
        }else{
            this.componentList[name] = config;
        }
    }

    /**
     * 注册一个指令
     * @param {*} name 
     * @param {*} config 
     */
    directive(name,config){
        if(this.directiveList[name]){
             console.log(name + '指令已经存在');
        }else{
            this.directiveList[name] = config;
        }
    }


    /**
     * 创建一个应用程序
     * @param {*} Component 
     * @param {*} props 
     */
    createApp(Component,props){
        if (typeof(Vue) == 'undefined') {
            console.error('请先引入vue.js');
            return;
        }
        if (Component==undefined) Component= {};
         let defaultMixins = {
            data(){
                return {
                    ...this.data
                };
            },
            components:this.componentList,
            directives:this.directiveList,
            methods:{
                
            }
        }
        
        //混入data和mixin
        Component.mixins =[defaultMixins];
        let app = Vue.createApp(Component,props);
        app.use(biPlug,this);
        app.use(adminPlug,this);
        //obj.directive(vPower.name,vPower);

        return app;
    }

    setData(data){

        for(let key in data){
            
            switch(key){
                case '__appName':
                    this.app = data[key];    
                break;

                case '__actionName':
                    this.action = data[key];
                break;
                
                case '__controllerName':
                    this.controller = data[key];
                break;
                
                case '__user':
                    this.user.setData(data[key]);

                break;
                case '__uploadBaseUrl':
                    this.uploadBaseUrl = data[key];
                break;
                
                default:
                    this.data[key] = data[key];
                break;
            }
        }

        let currentPage = this.app + '-'+this.controller + '-'+ this.action;
        
        this.user.currentPage = currentPage

        this.status = new Status(currentPage);

        if (this.user.status == 'new'){
            window.onload = ()=>{this.modifyUserPassword(true);}
        }
    }

    url(config){
        let app = config['app']??this.app;
        let action = config['action']??'index';
        let controller = config['controller']??this.controller;
        let gets = config['gets']??{};
        
        let url  = this.baseUrl + '/' + app + '/' + controller +'/' + action;

        for(let key in gets){
            url += '/' + key + '/' + gets[key];
        }
        return url;

    }

    go(url,back = false){

        if (typeof url == "number"){
            this.user.setBackStatus();
            window.history.go(url);
        }

        if (back){
            this.user.setBackStatus();
        }

        if (typeof url == "string"){
            location.href = url;
        }

        if (typeof url == "object"){
            location.href = this.url(url);
        }
    }

    getAppInfo(app = ''){
        
        if (app=='') app = this.app;
        
        let appList = this.user.getLocalStorage('appList');

        if (appList!==null){
            for(let i=0;i<appList.length;i++){
                if(appList[i].name==app){
                    return appList[i];
                }
            }
        }
        
        return {
            name:app,
            url:'',
            icon:'',
            title:'',
            description:'',
            version:'',
            author:'',
            admin_url:''
        };

    }

    userInit(callback = ()=>{}){
        ajax({
            url:this.url({'app':'admin','controller':'index','action':'userInit'}),
            success:(init)=>{
                
                this.user.setLocalStorage('appList',init.appList);
                callback();
            }
        })
    }

    modifyUserPassword(first = false){
        dialog({
            type:'vue',
            closeable:!first,
            content:this.createApp(modifyUserPasswordDialog,{first})
        })
    }

    userFile(callback,ext ='',app = ''){
        dialog({
            type:'vue',
            content:this.createApp(userFileDialog,{ext,app}),
            callback:(file)=>{
               if (typeof callback == 'function')  callback(file);
            }
        })
    }

    systemFile(callback,config={}){

        let  props = {
            folder:false,
            file:true,
            system:false,
            root:'/',
            rootIcon:'fa-home',
            multiple:false,
            ext:'*'
        }

        assign(props,config);

        dialog({
            title:config.title??'选择文件',
            type:'vue',
            content:this.createApp(systemFileDialog,props),
            callback:(value)=>{
               if (typeof callback == 'function')  callback(value);
            }
        })
    }


    //注册或者刷新
    refresh(name,fun){
        
        if (window.__refresh == undefined){
            window.__refresh = {};
        }

        if (name==undefined){
            for(let key in window.__refresh){
                window.__refresh[key]();
            }
        }


        if (fun ==undefined){
            if(window.__refresh[name] &&  typeof window.__refresh[name]  == "function"){
                window.__refresh[name]();
            }
        }else{
            if (typeof fun == "function"){
                window.__refresh[name] = fun;            
            }            
        }
    }
}

export default admin;